package com.ww.springboot.boot.thread.study.base.join;

import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;

import static java.util.stream.Collectors.toList;

/**
 * join
 *
 * join是在主线程调用 join线程未结束生命周期前 主线程会一直等待下去
 *
 *
 * @author ZH500
 */
public class ThreadJoin {
    public static void main(String[] args) throws InterruptedException {
        //① 定义两个线程，并保存在threads中
        List<Thread> threads = IntStream.range(1, 3).mapToObj(ThreadJoin::create).collect(toList());
        //② 启动这两个线程
        threads.forEach(Thread::start);
        //③ 执行这两个线程的join方法
       /* for (Thread thread : threads) {
            thread.join();
        }*/
        System.out.println("join1");
        threads.get(0).join();
        System.out.println("join2");
        threads.get(1).join();
        System.out.println("join end");
        //④ main线程循环输出
        for (int i = 0; i < 10; i++) {
            System.out.println(Thread.currentThread().getName() + "#" + i);
            shortSleep();
        }
    }

    //构造一个简单的线程，每个线程只是简单的循环输出
    private static Thread create(int seq) {
        return new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                System.out.println(Thread.currentThread().getName() + "#" + i);
                shortSleep();
            }
        }, String.valueOf(seq));
    }

    private static void shortSleep() {
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            System.out.println(Thread.currentThread().getName()+":被中断了");
            e.printStackTrace();
        }
    }
}